﻿using System;
using System.Collections.Generic;
using System.Linq;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.Core.BLL
{
    public class AnswerListView
    {
        public int AnsId { get; set; }
        public string AnsName { get; set; }
        public int QuestionId { get; set; }
        public int Total { get; set; }
    }

    public class Answers
    {
        public static Answers Instance
        {
            get { return new Answers(); }
        }

        public static AnswerListView LoadDataFromListItem(Answer item)
        {
            var objAnswers = new AnswerListView
                                 {
                                     AnsName = item.AnsName,
                                     QuestionId = Convert.ToInt32(item.QuestionId),
                                     Total = Convert.ToInt32(item.Total)
                                 };

            return objAnswers;
        }

        /// <summary>
        /// Create Answer
        /// </summary>
        /// <param name="name">Answer Name</param>
        /// <param name="questionId">Question Id</param>
        /// <returns></returns>
        public bool Create(string name, int questionId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAnswers = new Answer
                                         {
                                             AnsName = Common.Instance.Truncate(name, 250, false, false),
                                             QuestionId = questionId,
                                             Total = 0
                                         };

                    dc.Answers.InsertOnSubmit(objAnswers);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Create Answer
        /// </summary>
        /// <param name="name">Answer Name</param>
        /// <param name="total">Total Vote</param>
        /// <param name="questionId">Question Id</param>
        /// <returns></returns>
        public bool Create(string name, int total, int questionId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAnswers = new Answer
                                         {
                                             AnsName = Common.Instance.Truncate(name, 250, false, false),
                                             QuestionId = questionId,
                                             Total = total
                                         };

                    dc.Answers.InsertOnSubmit(objAnswers);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Answer
        /// </summary>
        /// <param name="answerId">Answer Id</param>
        /// <param name="name">Answer Name</param>
        /// <param name="total">Total Vote</param>
        /// <returns></returns>
        public bool Update(int answerId, string name, int total)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAnswer = (dc.Answers.Where(p => p.AnsID == answerId)).Single();
                    objAnswer.AnsName = Common.Instance.Truncate(name, 250, false, false);
                    objAnswer.Total = total;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update total vote
        /// </summary>
        /// <param name="answerId">Answer Id</param>
        /// <returns></returns>
        public bool UpdateVote(int answerId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAnswer = (dc.Answers.Where(p => p.AnsID == answerId)).Single();
                    objAnswer.Total = objAnswer.Total + 1;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete Answer
        /// </summary>
        /// <param name="answerId">Answer Id</param>
        /// <returns></returns>
        public bool Delete(int answerId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objAnswer = (dc.Answers.Where(p => p.AnsID == answerId)).Single();

                    dc.Answers.DeleteOnSubmit(objAnswer);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Get Answer by Question
        /// </summary>
        /// <param name="questionId">Question Id</param>
        /// <returns>List answer item</returns>
        public List<AnswerListView> GetByQuestion(int questionId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = from a in dc.Answers
                            where a.QuestionId == questionId
                            select new AnswerListView
                            {
                                AnsId = a.AnsID,
                                AnsName = a.AnsName,
                                QuestionId = Convert.ToInt32(a.QuestionId),
                                Total = Convert.ToInt32(a.Total)
                            };

                return query.ToList();
            }
        }
    }
}
